534B - Covered Path - CodeForces Solution


dp greedy math *1400

Please click on ads to support us..

Python Code:

import sys

def get_int():
    return int(sys.stdin.readline().strip())

def get_ints():
    return map(int,sys.stdin.readline().strip().split())

def get_list():
    return list(map(int,sys.stdin.readline().strip().split()))

def get_string():
    return sys.stdin.readline().strip()


v1,v2 = get_ints()
t,d = get_ints()


    
    

dp = [[float('-inf')]*(1001) for _ in range(t+1)]

for j in range(1000,0,-1):
    if abs(v2-j)<=d:
        dp[t][j] = v2

for i in range(t-1,0,-1):
    for j in range(1000,0,-1):
        for k in range(-d,d+1):
            if j+k>=0 and j+k<=1000:
                dp[i][j] = max(dp[i][j], j+k + dp[i+1][j+k])


print(v1 + dp[2][v1])

C++ Code:

#include<bits/stdc++.h>
using namespace std;

#define MX -999999
int step,d,endi,starti;

//struct direct{
//    int s, t;
//    direct(){
//    }
//
//    direct(int _s, int _t){
//        s=_s;
//        t=_t;
//    }
//
//}dir[10000][10000];

int dp[1000][1000];

int call(int start, int t){

    if(start-(step-t)*d>endi)
        return MX;

    if(dp[start][t]!=-1)
        return dp[start][t];

    int maxi=MX;

    if(t>=step && start==endi)
        return 0;
    if(t>=step)
        return MX;


    for(int i=start;i<=start+d;i++){
        if(i+call(i,t+1)>maxi){
            maxi=i+call(i,t+1);
//            dir[start][t]= direct(i,t+1);
        }
    }

    for(int i=start;i>=start-d;i--){
        if(i>0){
            if(i+call(i,t+1)>maxi){
                maxi=i+call(i,t+1);
//                dir[start][t]= direct(i,t+1);
            }
        }
    }

    return dp[start][t]=maxi;

}

//void print_path(int s,int t){
//    if(dir[s][t].s==-1)
//        return;
//
//    int next_s= dir[s][t].s;
//    int next_i=dir[s][t].t;
//
//    cout<<next_s<<endl;
//
//    print_path(next_s, next_i);
//
//
//}

main(){
    cin>> starti>> endi>>step>>d;
//    memset(dir, -1, sizeof dir);
    memset(dp, -1, sizeof dp);

    int dist= starti+ call(starti,1);


//    cout<<starti<<endl;
//    print_path(starti,1);
    cout<< dist<< endl;


}


Comments

Submit
0 Comments
More Questions

Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array